package com.company.fiveeight;

import java.util.*;

public class Main1 {
    public static void main(String[] args) {
        int[] data = {1,1,1,2,1};
        int[] res = removeDuplicate(data);
        System.out.println(Arrays.toString(res));
    }

    public static int[] removeDuplicate (int[] array) {
        // write code here
        if(array == null || array.length == 0){
            return new int[0];
        }
        Set<Integer> s = new HashSet<>();
        int len = array.length;
        List<Integer> list = new ArrayList<>();
        for (int i = len - 1; i >= 0 ; i--) {
            if (!s.contains(array[i])) {
                list.add(0, array[i]);
                s.add(array[i]);
            }
        }
        int size = list.size();
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = list.get(i);
        }
        return res;


    }
}
